﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class application_step2 : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        LoadCountry();
        setAttributes();
        LoadTime();
        btn_add_more_loading.Value = DAO.TextBaseFactory.loadText("add_more", this.LangId);
        btn_add_more_unloading.Value = DAO.TextBaseFactory.loadText("add_more", this.LangId);
        btn_save_loading.Value = DAO.TextBaseFactory.loadText("save", this.LangId);
        btn_save_unloading.Value = DAO.TextBaseFactory.loadText("save", this.LangId);
        btn_delete_loading.Value = DAO.TextBaseFactory.loadText("delete", this.LangId);
        btn_delete_unloading.Value = DAO.TextBaseFactory.loadText("delete", this.LangId);
        ShowHideFields();
    }

    private void ShowHideFields()
    {
        string type_offer_id = "";
        string type_transport_id = "";
        tbPostCodeLoading_rfv.Visible = false;
        tbPostCodeUnloading_rfv.Visible = false;
        if(Request.QueryString["type_transport_id"]!=null && Request.QueryString["type_offer_id"]!=null)
        {
            type_transport_id = Request.QueryString["type_transport_id"].ToString();
            type_offer_id = Request.QueryString["type_offer_id"].ToString();
            if (type_offer_id == "71")//Tender
            {
                tbDateClosing.Attributes.Add("validate", "true");
                tbDateOpening.Attributes.Add("validate", "true");
                //tbDateLoading_rfv.Visible = false;
                //tbDateUnloading_rfv.Visible = false;
                row_tender_app.Visible = true;
                row_tender_app_header.Visible = true;
                tbDateLoading.Attributes.Add("validate", "true");
                tbDateUnloading.Attributes.Add("validate", "true");
            }
            else
            {
                if (type_offer_id == "70")//Transport
                {
                    tbDateLoading.Attributes.Add("validate", "true");
                    tbDateUnloading_rfv.Visible = false;
                }
                else if (type_offer_id == "69")//Cargo
                {
                    tbDateLoading.Attributes.Add("validate", "true");
                    tbDateUnloading.Attributes.Add("validate", "true");
                }
            }
            switch (type_transport_id)
            {
                case "164"://Road
                case "166"://Rail
                break;
                case "165"://Sea
                    lbl_seaport_loading.Visible = true;
                    ddlSeaPortLoading.Visible = true;
                    img_seaport_loading.Visible = true;
                    lbl_seaport_unloading.Visible = true;
                    ddlSeaPortUnloading.Visible = true;
                    img_seaport_unloading.Visible = true;
                break;
                case "167"://Air
                    lbl_aiport_loading.Visible = true;
                    ddlAirportLoading.Visible = true;
                    img_aiport_loading.Visible = true;
                    lbl_aiport_unloading.Visible = true;
                    ddlAirportUnloading.Visible = true;
                    img_aiport_unloading.Visible = true;
                break;
            }
            if (type_offer_id == "70")//Type Offer => Transport required fields
            {
                //tbDateLoading.Attributes.Remove("validate");
                tbDateUnloading.Attributes.Remove("validate");
                ddlCountryUnloading.Attributes.Remove("validate");
                tbCityUnloading.Attributes.Remove("validate");
                tbPostCodeUnloading.Attributes.Remove("validate");
                ddlCountryUnloading_rfv.Visible = false;
                tbCityUnloading_rfv.Visible = false;
                tbPostCodeUnloading_rfv.Visible = false;
                tbPostCodeUnloading_rfv.Visible = false;
                tbDateUnloading_rfv.Visible = false;
            }
        }
    }

    private void LoadTime()
    {
        tbTimeLoading.Items.Clear();
        tbTimeUnloading.Items.Clear();
        tbTimeLoading.Items.Add(new ListItem("", "0"));
        tbTimeUnloading.Items.Add(new ListItem("", "0"));
        for (int i = 12; i > 0; i--)
        {
            if (i < 10)
            {
                tbTimeLoading.Items.Add(new ListItem("0" + i.ToString() + (":00"), i.ToString() + ":00"));
                tbTimeUnloading.Items.Add(new ListItem("0" + i.ToString() + ":00", i.ToString() + ":00"));
            }
            else 
            {
                tbTimeLoading.Items.Add(new ListItem(i.ToString() + (":00"), i.ToString() + ":00"));
                tbTimeUnloading.Items.Add(new ListItem(i.ToString() + ":00", i.ToString() + ":00"));
            }
        }
    }

    private void setAttributes()
    {
        ddlCountryLoading.Attributes.Add("validate", "true");
        ddlCountryUnloading.Attributes.Add("validate", "true");
        tbCityLoading.Attributes.Add("validate", "true");
        tbCityUnloading.Attributes.Add("validate", "true");
        //tbPostCodeLoading.Attributes.Add("validate", "true");
        //tbPostCodeUnloading.Attributes.Add("validate", "true");
        tbDateLoading.Attributes.Add("readonly", "true");
        tbDateUnloading.Attributes.Add("readonly", "true");
        //ddlAirportLoading.Attributes.Add("validate", "true");
        //ddlAirportUnloading.Attributes.Add("validate", "true");
        //ddlSeaPortLoading.Attributes.Add("validate", "true");
        //ddlSeaPortUnloading.Attributes.Add("validate", "true");
        
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }

    private void LoadCountry()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetAllPlaces(0, 1000000, 'id', 'asc', 2) WHERE isActive='true' Order By name asc", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddlCountryLoading.Items.Add(new ListItem("", "0"));
                ddlCountryUnloading.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlCountryLoading.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                    ddlCountryUnloading.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }
}


